package top.letscode.adclicker;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.openqa.selenium.WebDriver;

import top.letscode.adclicker.ad.AbstractUnion;
import top.letscode.adclicker.annotation.AnnotationUtil;
import top.letscode.adclicker.driver.ChromeDriverFactory;
import top.letscode.adclicker.executor.AdExecutor;
import top.letscode.adclicker.site.AbstractSite;
import top.letscode.adclicker.store.SimpleDataStore;
import top.letscode.adclicker.util.AdslDialer;
import top.letscode.adclicker.util.Constants;
import top.letscode.adclicker.util.RandomUtil;
import top.letscode.adclicker.util.Utils;

/**
 * 主程序入口
 */
public class Application {

  private static long LAST_CHECK_TIME = 0;
  // 路径写死，服务器上必须放在C盘下，这样在本地执行的时候不会执行clear.bat
  private static File CLEAR_FILE = new File("c:/clear.bat");
  private static String CLEAR_COMMAND = "cmd /c start c:/clear.bat";

  private static void clearTpmFile() {
    try {
      if (!CLEAR_FILE.exists()) {
        return;
      }
      System.out.println("start clear tmp files......");
      Process ps = Runtime.getRuntime().exec(CLEAR_COMMAND);
      ps.waitFor();
      System.out.println("end clear tmp files......");
    } catch (Exception e) {// ignore
      e.printStackTrace();
    }
  }

  private static void startAlexA() {
    WebDriver webDriver = ChromeDriverFactory.newInstance(Constants.UNION_TYPE_WEB);
    List<String> siteList =
        Arrays.asList("www_letscode_top.txt", "www_letsjoke_cn.txt", "www_myitstudio_cn.txt");
    while (true) {
      try {
        String site = RandomUtil.random(siteList);
        List<String> urls = (List<String>) SimpleDataStore
            .getFileLines(Constants.STORE_ANNOTATION_SITE, "/urls/" + site);
        webDriver.get(RandomUtil.random(urls));
        Utils.sleepQuietly(3 + RandomUtil.randomInt(2));
      } catch (Exception e) {

      }
    }
  }

  public static void main(String[] args) throws Exception {
    // 每次重启都先休眠几秒钟，解决系统重启时环境还没初始好就启动程序出错的问题
    Utils.sleepQuietly(5);
    // 默认myitstudio.cn，有指定执行信息的手，格式需要为siteName:unionName;siteName:unionName
    String[] executeArr = new String[] {"www.myitstudio.cn:a5", "www.letscode.top:x3g",
        "m.xiaohua.myitstudio.cn:qilin"};
    if (args != null && args.length > 0) {
      executeArr = args[0].split(";");
    }
    // 等待url资源加载完毕
    Utils.sleepQuietly(1);
    for (String executeInfo : executeArr) {
      AbstractSite site = AnnotationUtil.getSite(executeInfo.split(":")[0]);
      AbstractUnion union = AnnotationUtil.getUnion(executeInfo.split(":")[1]);

      new Thread(new AdExecutor(union, site)).start();
    }

    // 启动刷alexa排名的线程
    // startAlexA();

    // 主线程每隔几秒检测当前打开的WebDriver是否有弹窗导致阻塞的情况，有的话及时关闭
    while (true) {
      System.out.println("programe is running...");
      try {
        AdslDialer.connect();
      } catch (Exception e) {

      }

      if (System.currentTimeMillis() - LAST_CHECK_TIME > 10 * 60 * 1000) {
        clearTpmFile();
        LAST_CHECK_TIME = System.currentTimeMillis();
      }

      Utils.sleepQuietly(30);
    }
  }
}
